import os, time
from Tkinter import *

class main():
    def __init__(self, parent, root):
        
        self.root = root
        self.root.title('View Entire Inventory')
        self.boothPath = parent.boothPath
        self.parent = parent      
        self.dh = parent.dh
        cons_nums = []
        for dirs in os.listdir('./Store/Booths/'):
            cons_nums.append(int(dirs))
            
        max_assigned = max(cons_nums)
        self.consignor = StringVar()
        self.consignor.set('all')

        self.status = StringVar()
        self.status.set('Enter consignor by name or number and click Search to get started')
        
        kwLabel = Label(self.root,text= 'Consignor : ').grid(row=0,column=0,sticky=W)
        kwEntry = Entry(self.root, textvariable = self.consignor, width = 20)
        kwEntry.grid(row=0,column=1)
        kwEntry.bind('<Return>',self.searchByKw)
        kwEntry.bind('<KP_Enter>',self.searchByKw)
        kwEntry.focus()

        
        findByKw = Button(self.root, text='Search',command=self.searchByKw, width=10).grid(row=0,column=2)
        self.resultsBox = Listbox(self.root,width=80,height=30)
        self.resultsBox.grid(row=1,column=0,columnspan=3)
        self.resultsBox.bind('<Double-Button-1>',self.addToSale)
        statusLabel = Label(self.root,textvariable =self.status).grid(row=2,column=0,columnspan=2)
        doneButton = Button(self.root,text=' Done ', command=self.finalize, width=10).grid(row=3,column=2,sticky=E)

            
    def searchByKw(self,event=None):
       
        consignor = self.consignor.get()
        def findAll():
            self.resultsBox.delete(0,END)
            for key in self.dh.cons_dict.keys():
                for item_key in self.dh.cons_dict[key].item_dict.keys():
                    item = self.dh.cons_dict[key].item_dict[item_key]
                    insText = '%s  %s   $%s   Owner: %s Description: %s' % (item.owner_number, item.item_number, item.sale_price, item.owner, item.description)
                    self.resultsBox.insert(END, insText)
        def findByNum(cons_id):
            self.resultsBox.delete(0,END)
            if cons_id in self.dh.cons_dict.keys():
                self.status.set('Items displayed')
                for item_key in self.dh.cons_dict[cons_id].item_dict.keys():
                    item = self.dh.cons_dict[cons_id].item_dict[item_key]
                    insText = '%s  %s   $%s   Owner: %s Description: %s' % (item.owner_number, item.item_number, item.sale_price, item.owner, item.description)
                    self.resultsBox.insert(END, insText)
            else:
                self.status.set('Consignor number not found')                       
        def findByName(cons_id):
            cons = self.dh.find_consignor_by_name(cons_id)
            if cons == None:
                self.status.set('Consignor name not found')
            else:
                self.status.set('Consignor items displayed')
                findByNum(cons.number)
                
        if consignor == 'all':
            findAll()
        else:
            try:
                findByNum('%i' % int(consignor))
            except:
                findByName(consignor)
                
            
                
 
        
    def addToSale(self,event=None):   
        anchor = self.resultsBox.curselection()
        data = self.resultsBox.get(anchor)
        data = data.split()
        booth,item,price = data[0],data[1],data[2].strip('$')
        price = int(float(price)*100)
        lkpItem = '%s %s %s' % (booth, item, price)
        self.parent.readBarcode.set(lkpItem)
        self.parent.lookupItem()
    
    def finalize(self):

        self.root.destroy()
            
        

def start(parent,root):
        
    ins = main(parent,root)
